<template>
    <div>
        <!--操作区-->
        <div>
            <el-form :inline="true" :model="query" class="demo-form-inline">
                <el-form-item label="">
                    <el-input v-model="query.LoginName" placeholder="用户名模糊查询"></el-input>
                </el-form-item>
                <!--<el-form-item label="">
                    <el-select v-model="query.Status" placeholder="状态">
                        <el-option label="区域一" value="shanghai"></el-option>
                        <el-option label="区域二" value="beijing"></el-option>
                    </el-select>
                </el-form-item>
                -->
                <el-form-item>
                    <el-button type="primary" @click="onSubmit" icon="el-icon-search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="danger" @click="onDelete" icon="el-icon-delete-solid">批量删除</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="warning" @click="jumpUpdate">修改</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" @click="dialogTableVisible = true">添加</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-container>
            <el-aside width="200px">
                <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
            </el-aside>
            <el-main>
                <!--显示用户列表-->
                <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55"> </el-table-column>
                    <el-table-column fixed prop="NickName" label="账号" width="150"> </el-table-column>
                    <el-table-column prop="LoginName" label="姓名" width="120"> </el-table-column>
                    <el-table-column label="角色" width="120">
                        <template slot-scope="scope">
                            <el-tag type="primary" v-for="(t, index) in scope.row.Role" :key="index">{{ t.RoleName }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="Status" label="状态" width="120">
                        <template slot-scope="scope">
                            <span v-if="scope.row.Status == 0">正常</span>
                            <span v-if="scope.row.Status == 1">禁用</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="address" label="创建时间" width="300"> </el-table-column>
                    <el-table-column fixed="right" label="操作" width="200">
                        <template slot-scope="scope">
                            <el-button @click="handleClick(scope.row)" type="danger" icon="el-icon-delete-solid" size="small"
                                >删除</el-button>
                            <el-button type="warning" icon="el-icon-edit" size="small">编辑</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-main>
        </el-container>
        <!--分页-->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="Pageindex"
            :page-sizes="[1, 2, 3, 4]"
            :page-size="Pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="recordCount"
        >
        </el-pagination>
        <!--添加账户-->
        <div>
            <el-dialog title="添加账号" :visible.sync="dialogTableVisible">
                <el-form :model="form">
                    <el-form-item label="用户账户">
                        <el-input v-model="form.LoginName"></el-input>
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input type="password" v-model="form.Password"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码">
                        <el-input type="password" v-model="form.paws"></el-input>
                    </el-form-item>
                    <el-form-item label="启用/禁用">
                        <el-switch v-model="form.Status" :active-value="0" :inactive-value="1"></el-switch>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="onAdd()">确 定</el-button>
                </div>
            </el-dialog>
        </div>
        <!--修改账户-->
        <div>
            <el-dialog title="修改账号" :visible.sync="EditVisible">
                <el-form :model="editform">
                    <el-form-item label="真实姓名" prop="NickName">
                        <el-input v-model="editform.NickName"></el-input>
                    </el-form-item>
                    <el-form-item label="用户账户" prop="LoginName">
                        <el-input v-model="editform.LoginName"></el-input>
                    </el-form-item>
                    <el-form-item label="角色" prop="Role">
                        <el-select v-model="editform.Roles" multiple placeholder="请选择" @change="xin($event)">
                            <el-option v-for="(t, index) in roleList" :key="index" :label="t.Name" :value="t.Id"> </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="启用/禁用" prop="Status">
                        <el-switch v-model="editform.Status" :active-value="0" :inactive-value="1"></el-switch>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="EditVisible = false">取 消</el-button>
                    <el-button type="primary" @click="onUpdate()">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>
<script>
import { UserQuery, UserInsert, UserUpdate, UserDelete } from '@/api/User.js';
import { RolesQuery } from '@/api/Roles.js';
import { DepartmentQuery } from '@/api/Department.js';

export default {
    methods: {
        //删除
        handleClick(row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    UserDelete(row).then((res) => {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.UserShow();
                    });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        //查询
        onSubmit() {
            this.UserShow();
        },
        //左侧树形筛选
        handleNodeClick(data) {
            this.UserShow(data.Id);
            console.log(data);
        },
        //查询方法
        getTreeData() {
            DepartmentQuery().then((res) => {
                //console.log(res)
                this.data = res.Data;
            });
        },
        //显示+查询+分页
        UserShow(Id) {
            let data = {
                DepartmentId:Id,
                Pageindex: this.Pageindex,
                Pagesize: this.Pagesize,
                UserName: this.query.LoginName
            };
            UserQuery(data).then((res) => {
                //console.log(res);
                this.tableData = res.Data.plist;
                this.recordCount = res.Data.recordCount;
            });
        },
        //修改弹出
        jumpUpdate() {
            //判断是否存在多选框是否有被选中的
            if (this.multipleSelection == null) {
                this.$message.warning('请选择一项再进行修改');
                return;
            } else if (this.multipleSelection.length != 1) {
                this.$message.warning('只能选择一项用户信息进行修改');
                return;
            }
            //清空原来的值
            this.editform = {};
            //为表单反填信息
            this.editform = this.multipleSelection[0];
            let array = [];
            this.multipleSelection[0].Role.forEach((e) => {
                array.push(e.RoleId);
            });
            this.editform.Roles = array;
            //弹出表单
            this.EditVisible = true;
        },
        //修改
        onUpdate() {
            console.log(this.editform);
            UserUpdate(this.editform).then((res) => {
                if (res.Code == 200) {
                    //修改成功
                    this.$message.success(res.Message);
                    this.EditVisible = false;
                    this.onSubmit();
                } else {
                    this.$message.error(res.Message);
                }
            });
        },
        //获取角色信息
        GetRoles() {
            RolesQuery().then((res) => {
                this.roleList = res.Data.plist;
                //console.log(this.roleList)
            });
        },
        //添加
        onAdd() {
            UserInsert(this.form).then((res) => {
                if (res.Code == 200) {
                    //添加成功
                    this.$message.success(res.Message);
                    this.dialogTableVisible = false;
                    this.onSubmit();
                } else {
                    this.$message.error(res.Message);
                }
            });
        },
        //删除
        onDelete() {
            //判断是否存在多选框是否有被选中的
            if (this.multipleSelection == null) {
                this.$message.warning('请选择一项再进行删除');
                return;
            } else if (this.multipleSelection.length != 1) {
                this.$message.warning('只能选择一项部门信息进行删除');
                return;
            }
            let id;
            id = this.multipleSelection[0].Id;
            this.$confirm('你确定要删除该用户信息吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    UserDelete({ Id: id }).then((res) => {
                        if (res.Code == 200) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.UserShow();
                        } else {
                            this.$message.error(res.Message);
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        //分页
        handleSizeChange(val) {
            this.query.Pagesize = val;
            this.UserShow();
        },
        handleCurrentChange(val) {
            this.Pageindex = val;
            this.UserShow();
        },
        //多选
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        xin() {
            this.$forceUpdate();
        }
    },
    created() {
        this.UserShow();
        this.GetRoles();
        this.getTreeData();
    },
    data() {
        return {
            tableData: [],
            tableDatas: [],
            RoletableData: [],
            query: {
                Pageindex: 1,
                Pagesize: 5,
                UserName: ''
            },
            multipleSelection: [],
            multipleSelections: [],
            dialogTableVisible: false,
            dialogVisibles: false,
            form: {
                LoginName: '',
                Password: ''
            },
            Pageindex: 1,
            Pagesize: 3,
            recordCount: 0,
            multipleSelection: [],
            editform: { DepartmentId: 999 },
            EditVisible: false,
            roleList: [],
            data: [],
            defaultProps: {
                children: 'children',
                label: 'Name'
            }
        };
    }
};
</script>
  